var express = require('express')
var router = express.Router();
const { uploadFiles, moveFiles, removeFiles }=require('../../public/network/handleFiles');
const { getAdmin,addAdmin,queryAdmin,reviseAdmin,delAdmin ,getSuperAdmin,getPicture,addPicture} = require('../../service/admin/adminService');
//渲染页面
router.post('/getAdmin', async function (req, res, next) {
    const {  currentPage, pageSize } = req.body;
    const data = await getAdmin({ currentPage,pageSize });
    console.log({  currentPage, pageSize })
    // res.jsonp(data);
    res.send(data);
});
//新增
router.post('/addAdmin', async function (req, res, next) {
    const {name,pirce,shopid,bool,stock,img} = req.body;
    const data = await addAdmin({name,pirce,shopid,bool,stock,img:`./images/${img}`});
    if(data.success){
        moveFiles({
          fromPath:'./public/temp',
          toPath:'./public/images',
          filename:img
        });
        removeFiles('./public/temp');
        res.send(data);
      }else{
        res.send(data);
      }
});
//删除
router.post('/delAdmin', async function (req, res, next) {
    const {_id} = req.body;
    const data = await delAdmin({_id});
    res.send(data);
});
//查询
router.post('/queryAdmin', async function (req, res, next) {
    const { choose,value } = req.body;
    const data = await queryAdmin({ choose,value });
    res.send(data);
});
//修改
router.post('/reviseAdmin', async function (req, res, next) {
    const { _id, shopid, name, pirce, bool, stock } = req.body;
    const data = await reviseAdmin({ _id, shopid, name, pirce, bool, stock });
    res.send(data);
});
//图片
router.post('/addImage', async function (req, res, next) {
    const getImages =   uploadFiles({
        path:'./public/temp',
        key:'file',
        size:10000
      });
      getImages(req,res,err=>{
         if(err){
           console.log('图片上传失败');
           console.log(err);
         }else{
           res.send({msg:'图片上传成功',
           success:true,
           filename:req.files.map(item=>item.filename)  //为图片的路径
         })
         }
      });

});

/********************************************************/
//超级管理员 渲染
router.post('/getSuperAdmin', async function (req, res, next) {
  const { currentPage, pageSize } = req.body;
  const data = await getSuperAdmin({ currentPage,pageSize });
  console.log({  currentPage, pageSize })
  // res.jsonp(data);
  res.send(data);
});
//轮播图渲染  getPicture
router.post('/getPicture', async function (req, res, next) {
  const data = await getPicture();
  // res.jsonp(data);
  res.send(data);
});
//轮播图图片管理
router.post('/addImg', async function (req, res, next) {

  const getImages =   uploadFiles({
      path:'./public/template',
      key:'file',
      size:10000
    });
    getImages(req,res,err=>{
       if(err){
         console.log('图片上传失败');
         console.log(err);
       }else{
         res.send({msg:'图片上传成功',
         success:true,
         filename:req.files.map(item=>item.filename)  //为图片的路径
       })
       }
    });

});
//添加轮播图
router.post('/addPicture', async function (req, res, next) {
  const {img} = req.body;
  const data = await addPicture({img:`./images/${img}`});
  if(data.success){
      moveFiles({
        fromPath:'./public/template',
        toPath:'./public/img',
        filename:img
      });
      removeFiles('./public/template');
      res.send(data);
    }else{
      res.send(data);
    }
});



module.exports = router